package gatperdut.chato.core.interpreter.grammar.comminfo;

import gatperdut.chato.core.interpreter.grammar.comminfo.indexedword.IndexedWord;

import java.util.ArrayList;
import java.util.List;

public class CommandInfo {

    public enum CommandType {
        SAY, TELL, SHOUT, GO, GET_ROOM, GET_CONTAINER, PUT, DROP, GIVE, LOOK, LOOK_AT, LOOK_IN, SPILL, SCORE, FILL, EMPTY, LOOK_WORN, CRAFT, FOOD, THROW, SPEAK, SETS, HIT, FLEE, STOP, EMOTE, MANIPULATION_DOOR, MANIPULATION_CONTAINER, JUNK, WEAR, REMOVE, STATUS, SKILLS, SET, SWITCH, LIST_ITEM_TEMPLATES, DOCUMENT, INVENTORY, HELP, EXITS, QUIT, COIR, SHUTDOWN, MUDSTATS;
    };

    private final CommandType commandType;
    private final List<IndexedWord> iWords = new ArrayList<IndexedWord>();
    private String emote = null;

    public CommandInfo( CommandType commandType ) {
        this.commandType = commandType;
    }

    public String[] getReplacements() {
        List<String> r = new ArrayList<String>();
        if ( iWords.size() == 0 ) {
            throw new IllegalArgumentException( "Getting iWords replacements: no iwords." );
        }
        for ( IndexedWord iw : iWords ) {
            if ( iw.getReplacement() == null ) {
                throw new IllegalArgumentException( "Getting iword '" + iw.getIndex() + "." + iw.getWord() + "' replacement: has not been set" );
            }
            r.add( iw.getReplacement() );
        }

        String[] array = new String[iWords.size()];
        r.toArray( array );
        return array;
    }

    public String replaceIWords( String... strings ) {
        StringBuilder sb = new StringBuilder( "" );
        if ( emote == null ) {
            throw new IllegalStateException( "Replacing iwords: emote is null." );
        }
        if ( strings.length != iWords.size() ) {
            throw new IllegalArgumentException( "Replacing iwords: number of strings does not match number of iwords." );
        }

        int lastIndex = 0;
        int newStringsIndex = 0;
        for ( IndexedWord iw : iWords ) {
            String subString = emote.substring( lastIndex, iw.getPos() );
            sb.append( subString );
            sb.append( strings[newStringsIndex] );
            newStringsIndex++;
            lastIndex += subString.length();
        }

        sb.append( emote.substring( lastIndex, emote.length() ) );

        return sb.toString();
    }

    public CommandType getCommandType() {
        return commandType;
    }

    public List<IndexedWord> getiWords() {
        return iWords;
    }

    public String getEmote() {
        return emote;
    }

    public void setEmote( String emote ) {
        this.emote = emote;
    }
}
